// pages/pots/post-detali/post-detail.js
var postsData = require('../../../data/pots_data.js');
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detaile: null,
    collected: false,
    postId: null,
    isMusic:false
  },
  onshowModel(event){
   wx.showActionSheet({
     itemList: [
       '分享微信好友',
       '分享到朋友圈',
       '分享到微博',
       '分享到qq'
     ],
     itemColor:'#405f80',
     success(res){
       console.log(res.tapIndex);
       console.log(res.cancle);
     }
   })
  },
  onCollected(event) {
    var postsCollected = wx.getStorageSync('posts_collected');
    var postCollected = postsCollected[this.data.postId];
    postCollected = !postCollected;
    postsCollected[this.data.postId] = postCollected;
    wx.setStorageSync('posts_collected', postsCollected)
    this.setData({
      collected: postCollected
    });
    wx.showToast({
      title:postCollected?'收藏成功':'取消收藏',
      icon: 'success',
      duration: 1000
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var postId = options.id;
    this.setData({
      postId: postId
    });
    console.log(this.data.postId)
    var cc = postsData.postList[postId];
    this.setData({
      detaile: cc
    })

    var postsCollected = wx.getStorageSync('posts_collected');
    if (postsCollected) {
      var postCollected = postsCollected[postId]
      if (postCollected) {
        this.setData({
          collected: postCollected
        })
      }

    } else {
      var postsCollected = {}
      postsCollected[postId] = false;
      wx.setStorageSync('posts_collected', postsCollected);

    }
    if (app.globaData.p_isMusic && app.globaData.p_MusicpostId===postId){
     this.setData({
       isMusic:app.globaData.p_isMusic
     })
    }
    this.musicLister();
  },
  musicLister(){
    var that = this
    wx.onBackgroundAudioPlay(function () {
      that.setData({
        isMusic: true
      })
      app.globaData.p_isMusic=true;
      app.globaData.p_MusicpostId=that.postId
    });
    wx.onBackgroundAudioPause(function () {
      that.setData({
        isMusic: false
      })
      app.globaData.p_isMusic = false;
      app.globaData.p_MusicpostId=null;
    })
  },
  onMusic(event){
    var postId=this.data.postId;
    var postMusic = postsData.postList[postId].music
    if (this.data.isMusic){
      wx.pauseBackgroundAudio();
      this.setData({
        isMusic:false
      })
    }else{
      wx.playBackgroundAudio({
        dataUrl: postMusic.url,
        title: postMusic.title,
        coverImgUrl: postMusic.coverImg
      })
      this.setData({
        isMusic: true
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})